<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回结果集中的列数</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="pdostatement.closecursor.html">« PDOStatement::closeCursor</a></li>
      <li style="float: right;"><a href="pdostatement.debugdumpparams.html">PDOStatement::debugDumpParams »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.pdostatement.html">PDOStatement</a></li>
    <li>返回结果集中的列数</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="pdostatement.columncount" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDOStatement::columnCount</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8, PECL pdo &gt;= 0.2.0)</p><p class="refpurpose"><span class="refname">PDOStatement::columnCount</span> &mdash; <span class="dc-title">
   返回结果集中的列数
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdostatement.columncount-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>PDOStatement::columnCount</strong></span>(): <span class="type">int</span></div>


  <p class="para rdfs-comment">
   使用 <span class="function"><strong>PDOStatement::columnCount()</strong></span> 返回由 PDOStatement 对象代表的结果集中的列数。
  </p>
  <p class="para">
   如果是由 <span class="function"><a href="pdo.query.html" class="function">PDO::query()</a></span> 返回的 PDOStatement 对象，则列数计算立即可用。
  </p>
  <p class="para">
  如果是由 <span class="function"><a href="pdo.prepare.html" class="function">PDO::prepare()</a></span> 返回的 PDOStatement 对象，则在调用 <span class="function"><a href="pdostatement.execute.html" class="function">PDOStatement::execute()</a></span> 之前都不能准确地计算出列数。
  </p>

 </div>

 <div class="refsect1 returnvalues" id="refsect1-pdostatement.columncount-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
    返回由 PDOStatement 对象代表的结果集中的列数。如果没有结果集，则 <span class="function"><strong>PDOStatement::columnCount()</strong></span> 返回 <code class="literal">0</code>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdostatement.columncount-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1061">
    <p><strong>示例 #1 计算列数</strong></p>
    <div class="example-contents"><p>
     下面例子演示如何使用 <span class="function"><strong>PDOStatement::columnCount()</strong></span> 操作一个结果集和一个空集。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'odbc:sample'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'db2inst1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ibmdb2'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$sth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;name,&nbsp;colour&nbsp;FROM&nbsp;fruit"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;&nbsp;计算一个（不存在）的结果集中的列数&nbsp;*/<br /></span><span style="color: #0000BB">$colcount&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">columnCount</span><span style="color: #007700">();<br />print(</span><span style="color: #DD0000">"Before&nbsp;execute(),&nbsp;result&nbsp;set&nbsp;has&nbsp;</span><span style="color: #0000BB">$colcount</span><span style="color: #DD0000">&nbsp;columns&nbsp;(should&nbsp;be&nbsp;0)\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;计算结果集中的列数&nbsp;*/<br /></span><span style="color: #0000BB">$colcount&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">columnCount</span><span style="color: #007700">();<br />print(</span><span style="color: #DD0000">"After&nbsp;execute(),&nbsp;result&nbsp;set&nbsp;has&nbsp;</span><span style="color: #0000BB">$colcount</span><span style="color: #DD0000">&nbsp;columns&nbsp;(should&nbsp;be&nbsp;2)\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Before execute(), result set has 0 columns (should be 0)
After execute(), result set has 2 columns (should be 2)
</pre></div>
    </div>
   </div>
  </p>
 </div>



 <div class="refsect1 seealso" id="refsect1-pdostatement.columncount-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="pdo.prepare.html" class="function" rel="rdfs-seeAlso">PDO::prepare()</a> - 准备要执行的语句，并返回语句对象</span></li>
    <li class="member"><span class="function"><a href="pdostatement.execute.html" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a> - 执行一条预处理语句</span></li>
    <li class="member"><span class="function"><a href="pdostatement.rowcount.html" class="function" rel="rdfs-seeAlso">PDOStatement::rowCount()</a> - 返回受上一个 SQL 语句影响的行数</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>